
package com.jf.cloud.multishop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.jf.cloud.api.feign.SearchSpuFeignClient;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.dto.ProductSearchLimitDTO;
import com.jf.cloud.common.product.vo.search.SpuSearchVO;
import com.jf.cloud.multishop.mapper.ShopCollectionMapper;
import com.jf.cloud.multishop.mapper.ShopExtensionMapper;
import com.jf.cloud.multishop.model.ShopCollection;
import com.jf.cloud.multishop.service.ShopCollectionService;
import com.jf.cloud.multishop.service.ShopDetailService;
import com.jf.cloud.multishop.vo.ShopCollectionVO;
import com.jf.cloud.multishop.vo.ShopExtensionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户店铺收藏记录
 *
 * @author zz
 * @date 2021-02-23 14:41:42
 */
@Service
public class UserCollectionShopServiceImpl implements ShopCollectionService {

    @Autowired
    private ShopCollectionMapper shopCollectionMapper;

    @Autowired
    private ShopExtensionMapper shopExtensionMapper;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private SearchSpuFeignClient searchSpuFeignClient;


    @Override
    public ShopCollectionVO getByCollectionId(Long collectionId) {
        return shopCollectionMapper.getByCollectionId(collectionId);
    }

    @Override
    public void save(ShopCollection userCollectionShop) {
        shopCollectionMapper.save(userCollectionShop);
    }

    @Override
    public void update(ShopCollection userCollectionShop) {
        shopCollectionMapper.update(userCollectionShop);
    }

    @Override
    public PageVO<ShopCollectionVO> getUserCollectionShopPageByUserIdAndShopName(PageDTO pageDTO, Long userId, String shopName) {
        PageVO<ShopCollectionVO> shopCollectionPage = PageUtil.doPage(pageDTO, () -> shopCollectionMapper.listUserCollectionShopPageByUserIdAndShopName(userId, shopName));
        if(CollectionUtil.isEmpty(shopCollectionPage.getList())){
            return shopCollectionPage;
        }
        List<Long> shopIds = shopCollectionPage.getList().stream().map(ShopCollectionVO::getShopId).collect(Collectors.toList());
        ProductSearchLimitDTO productSearchLimitDTO = new ProductSearchLimitDTO(Constant.SPU_SIZE_FIVE);
        productSearchLimitDTO.setShopIds(shopIds);
        List<SpuSearchVO> shopSpuList = searchSpuFeignClient.limitSpuList(productSearchLimitDTO).getData();
        Map<Long, List<SpuSearchVO>> spuMap = shopSpuList.stream().collect(Collectors.groupingBy(SpuSearchVO::getShopId));
        for (ShopCollectionVO shopCollectionVO : shopCollectionPage.getList()) {
            if(!spuMap.containsKey(shopCollectionVO.getShopId()) || CollectionUtil.isEmpty(spuMap.get(shopCollectionVO.getShopId()))){
                continue;
            }
            shopCollectionVO.setSpuSearchList(spuMap.get(shopCollectionVO.getShopId()));
        }
        return shopCollectionPage;
    }

    @Override
    public int countByShopIdAndUserId(Long shopId, Long userId) {
        return shopCollectionMapper.countByShopIdAndUserId(shopId,userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrCancel(Long shopId, Long userId) {
        if (Objects.isNull(shopDetailService.getByShopId(shopId))) {
            // 该店铺不存在
            throw new LuckException("该店铺不存在");
        }
        boolean isAdd = false;
        if (countByShopIdAndUserId(shopId,userId) > 0) {
            shopCollectionMapper.deleteByShopIdAndUserId(shopId,userId);
        } else {
            ShopCollection userCollectionShop = new ShopCollection();
            userCollectionShop.setUserId(userId);
            userCollectionShop.setShopId(shopId);
            shopCollectionMapper.save(userCollectionShop);
            isAdd = true;
        }
        // 修改店铺的收藏数量
        long changeNum = isAdd ? 1L : -1L;
        ShopExtensionVO shopExtensionVO = shopExtensionMapper.getByShopId(shopId);
        shopExtensionVO.setCollectionNum(shopExtensionVO.getCollectionNum() + changeNum);
        shopExtensionMapper.update(shopExtensionVO);
        return isAdd;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchCancel(List<Long> shopIds, Long userId) {
        boolean remove = false;
        for (Long shopId : shopIds) {
            // 修改店铺的收藏数量
            ShopExtensionVO shopExtensionVO = shopExtensionMapper.getByShopId(shopId);
            if (Objects.isNull(shopExtensionVO)) {
                continue;
            }
            if (countByShopIdAndUserId(shopId,userId) > 0) {
                remove = shopCollectionMapper.deleteByShopIdAndUserId(shopId,userId);
                shopExtensionVO.setCollectionNum(shopExtensionVO.getCollectionNum() - 1);
                shopExtensionMapper.update(shopExtensionVO);
            }
        }
        return remove;
    }
}
